//Gets the value inside the text box in the HTML
function getIndexOfBF(){
    return document.getElementById("indexOf").value;
}

//places index value into hidden objects
function setIndexOfBF() {
    document.getElementById("indexOf").value = getCurrentIndexBF();
}

//Gets the value of the hidden current index field
function getCurrentIndexBF() {
    return document.getElementById("currentIndex").innerHTML;
}

//Places given value in the hidden current index field 
function setCurrentIndexBF(index) {
    document.getElementById("currentIndex").innerHTML = index;
}

//Checks the index for being in the bounds and valid
function isValidBF(index) {
    if (index == null || index === "") {
        return "Field is empty";
    } else if (isNaN(index)) {
        return "Invalid input";
    } else if (index <= 0) {
        return "Out of Lower Bound";
    } else if (index > getSizeC()) {
        return "Out of Upper Bound";
    } else {
        return true;
    }
}

function resetErrMessageBF() {
    printErrorMessageBF("");
}

// prints an error to the HTML page
function printErrorMessageBF(message){
    document.getElementById("ErrorMessage").innerHTML = message;
}

// changes the index value in accourdance with passed "action"
function updateIndexBF(action) {
    var curInd;
    var validity;
	//Increment
    if (action == "++") {
        curInd = getCurrentIndexBF();
        curInd++;
	//Decrement
    } else if (action == "--") {
        curInd = getCurrentIndexBF();
        curInd--;
	//Error
    } else {
        printErrorMessageBF("Update Index passed unknown action");
        return;
    }
    
    processIndexBF(curInd);
}

function processIndexBF(index) {
    var validity = isValidBF(parseInt(index));
 
    if (validity == true) {
        setCurrentIndexBF(parseInt(index));
        setIndexOfBF()
        indexUpdateBF(index);
        resetErrMessageBF();
    } else {
        setIndexOfBF(); //In case indexOf field was changed, change will be undone.
        printErrorMessageBF(validity);
    }
}
// updates path and rebuilds the table
function indexUpdateBF(index)
{
    path = "*/*["+(parseInt(index))+"]";
    XMLtoIDC(path)
}
// updates path and rebuilds the entire table
function printAllTableBF(index)
{
    resetErrMessageBF();
    XMLtoIDC("*/*")
}

function onEnterBF(e) 
{
    if (e.keyCode == 13) 
	{
        processIndexBF(getIndexOfBF());
    }
}
